package com.mzkj.common.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.impl.crypto.MacProvider;

import java.security.Key;

/**
 * json web token 工具类
 * Created by wangxw on 2016/12/13.
 */
public class AccessTokenUtils {
    public static String createToken(String subject) {
        if (StringUtils.isEmpty(subject)) {
            return "";
        }
        Key key = MacProvider.generateKey();
        return Jwts.builder()
                .setSubject(subject)
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();
    }

    public static String createToken(String accountId, String clientType) {
        if (StringUtils.isEmpty(accountId) || StringUtils.isEmpty(clientType)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(accountId)
                .append(System.currentTimeMillis())
                .append(clientType);
        Key key = MacProvider.generateKey();
        return Jwts.builder()
                .setSubject(sb.toString())
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();
    }

    public static void main(String[] args) {
        System.out.println(AccessTokenUtils.createToken("joe"));
    }
}
